﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 新增学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubMenuAddStudent_Click(object sender, EventArgs e)
        {
            AddStudentForm addStudentForm = new AddStudentForm();
            addStudentForm.MdiParent = this;
            addStudentForm.Show();
        }

        private void SubMenuStudentList_Click(object sender, EventArgs e)
        {            
            if (!CheckForm(typeof(StudentListForm).Name))
            {
                StudentListForm studentListForm = StudentListForm.CreateInstance();
                studentListForm.MdiParent = this;
                studentListForm.Show();
            }
        }

        private bool CheckForm(string formName)
        {
            bool flag = false;
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == formName)
                {
                    flag = true;
                    form.Activate();
                    break;
                }
            }
            return flag;
        }

        private void subMenuAddClass_Click(object sender, EventArgs e)
        {
            AddClassForm addClassForm = new AddClassForm();
            addClassForm.MdiParent = this;
            addClassForm.Show();
        }

        private void subMenuClassList_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(ClassListForm).Name))
            {
                ClassListForm classListForm = new ClassListForm();
                classListForm.MdiParent = this;
                classListForm.Show();
            }
        }

        private void subMenuGradeList_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(GradeListForm).Name))
            {
                GradeListForm gradeListForm = new GradeListForm();
                gradeListForm.MdiParent = this;
                gradeListForm.Show();
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("您确定要退出系统吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.ExitThread(); //退出当前线程上的消息循环
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void MIExit_Click(object sender, EventArgs e)
        {
            Application.Exit();//调用MainForm_FormClosing()方法
        }
    }
}
